package com.gs.api.reptile.xiaomi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gs.api.reptile.Constant;
import com.gs.api.reptile.entity.WallpaperData;
import com.gs.api.reptile.util.HttpClientUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guoyunfei on 2021/9/4 2:09
 * @className XiaomiCollectData
 * @description TODO 采集小米数据
 * @Version 1.0
 */
public class XiaomiCollectData {


    /**
     * 获取壁纸信息数据
     * @param keyword
     * @return
     */
    public static Map<String,Object> getXiaomiWallpapers(String keyword,int startPage){

        // TODO 数据组装集合
        Map<String,Object> resultMap = new HashMap<>();
        // 壁纸集合
        List<Map<String ,Object>> list = new ArrayList<>();

        String res = HttpClientUtils.get(String.format(Constant.XIAOMI_TAG_API, keyword, startPage));
        JSONObject object = JSONObject.parseObject(res);

        String apiMessage = object.getString("apiMessage");
        int apiCode = object.getIntValue("apiCode");

        if("success".equals(apiMessage) && apiCode == 0){

            //获取数据包
            JSONObject apiData = object.getJSONObject("apiData");

            //文件下载地址
            String fileServer = apiData.getString("fileServer");
            resultMap.put("fileServer",fileServer);
            //获取数据包
            JSONArray cards = apiData.getJSONArray("cards");

            JSONObject jsonObject = cards.getJSONObject(0);

            //标题
            String title = jsonObject.getString("title");
            resultMap.put("title",title);

            //获取壁纸信息数据
            JSONArray products = jsonObject.getJSONArray("products");
            int size = products.size();
            for (int i = 0; i < size; i++) {
                Map<String,Object> map  = new HashMap<>(6);
                //原始壁纸地址
                String originalImageUrl = products.getJSONObject(i).getString("originalImageUrl");
                //压缩壁纸地址
                String imageUrl = products.getJSONObject(i).getString("imageUrl");
                //壁纸名称
                String name = products.getJSONObject(i).getString("name");
                map.put("originalImageUrl",originalImageUrl);
                map.put("imageUrl",imageUrl);
                map.put("name",name);

                list.add(map);
                resultMap.put("list",list);
            }
            return resultMap;
        }

        return null;
    }

    /**
     * 数据封装
     * @param keyword 关键字
     * @return
     */
    public static List<WallpaperData> getWallpapers(String keyword,int startPage){

        Map<String, Object> objectMap = XiaomiCollectData.getXiaomiWallpapers(keyword,startPage);

        List<WallpaperData> list = JSONArray.parseArray(JSON.toJSONString(objectMap.get("list")), WallpaperData.class);

        if(null == list){
            return null;
        }

        for (WallpaperData data : list){
            data.setFileServer(objectMap.get("fileServer").toString());
            data.setTitle(objectMap.get("title").toString());
        }

        return list;
    }


    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            List<WallpaperData> dataList = XiaomiCollectData.getWallpapers("美女",0);

            for (int j = 0; j < dataList.size(); j++) {
                System.out.println(dataList.get(j).getFileServer());
                System.out.println(dataList.get(j).getImageUrl());
            }
        }
    }


}
